import api from "../common/api";

export const userInfoBehavior = Behavior({
  methods: {
    // 获取我的名字
    getName(sessionUser) {
      let realName = sessionUser.realName;
      let isInit = /经纪人[0-9]{11}/.test(realName);
      if (!isInit) {
        return realName;
      }
      return sessionUser.extProps.nickname || realName;
    },
    // 获取我的头像
    getAvatar(sessionUser) {
      return sessionUser.avatar || sessionUser.extProps.wxHeadImg;
    },

    _urlValid(url) {
      return url && url.indexOf("http") > -1;
    },

    /**
     * 格式化认证信息
     * @param {*} info 
     * return 
     *  avatar 头像
     *  mobile 手机号
     *  unionId 
     *  nick 昵称
     *  storeName 店铺名字
     */
    formatAuthenticatedInfo(info) {
      let result = {};
      result.avatar = this._urlValid(info.avatar) ? info.avatar : info.wxHeadImg;
      result.mobile = info.mobile || info.wxMobile;
      result.unionId = info.wechatUnionid;
      result.id = info.id //房源方id
      result.declaration = info.declaration
      result.beAllComplaintNum = info.beAllComplaintNum //被投诉次数
      result.callSuccessRatio = info.callSuccessRatio //接通率
      result.averageIMReplyTime = info.averageIMReplyTime //响应时间
      result.isVerified = info.isVerified //身份认证
      if (info.isVerified && info.bossEmployeeVO) {
        result.nick = info.bossEmployeeVO.realname;
        result.storeName = info.bossEmployeeVO.shopShortName;
        result.storeAddress = info.bossEmployeeVO.storeAddress; //门店地址
        result.attestation = info.bossEmployeeVO.attestation//判断房源详情是不是企业认证
      } else {
        result.nick = /经纪人[0-9]{11}/.test(info.realName)
          ? info.nickName || info.realName
          : info.realName || info.nickName;
        result.storeName = info.acStore.storeName;
      }
      return result;
    },

    // 通过unionid获取认证的信息
    getAuthenticatedInfoByUnionid(unionId) {
      return new Promise((resolve, reject) => {
        if (!unionId) {
          console.log('通过unionid获取认证的信息，没有参数');
          return reject()
        }
        api.getJJRVO({ unionId }, ({ data }) => {
          resolve(this.formatAuthenticatedInfo(data.content));
        });
      });
    },
  },
});
